<?php
/**
 * KayPaste
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category  KayPaste
 * @package   Models
 * @copyright Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author    Robin Skoglund
 */

/**
 * This class represents a paste.
 * 
 * @category  KayPaste
 * @package   Models
 * @copyright Copyright (c) 2007-2008 Robin Skoglund (http://robinsk.net/)
 * @license   http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author    Robin Skoglund
 */
class Kaypaste_Paste extends Doctrine_Record
{
    /**
     * Sets up table definition
     * 
     * @return void
     */
    public function setTableDefinition()
    {
        $this->setTableName('kaypaste__pastes');
        
        $this->option('collate', 'utf8_unicode_ci');
        $this->option('charset', 'utf8');
        $this->option('type', 'INNODB');
        
        // paste id
        $this->hasColumn('paste_id', 'integer', 3,
                         array(
                            'unsigned' => 0,
                            'primary' => true,
                            'notnull' => true,
                            'autoincrement' => true
                         ));

        // paste type (php, c, html, etc.)
        $this->hasColumn('type', 'string', 16,
                         array(
                            'fixed' => false,
                            'primary' => false,
                            'notnull' => false,
                            'autoincrement' => false
                         ));

        // paste description
        $this->hasColumn('description', 'string', 128,
                         array(
                            'fixed' => false,
                            'primary' => false,
                            'notnull' => false,
                            'autoincrement' => false
                         ));

        // optional password protection
        $this->hasColumn('password', 'string', 32,
                         array(
                            'fixed' => false,
                            'primary' => false,
                            'notnull' => false,
                            'autoincrement' => false
                         ));

        // paste author
        $this->hasColumn('author', 'string', 64,
                         array(
                            'fixed' => false,
                            'primary' => false,
                            'notnull' => false,
                            'autoincrement' => false
                         ));

        // optional reference to user->username
        $this->hasColumn('username', 'string', 16,
                         array(
                            'fixed' => false,
                            'primary' => false,
                            'notnull' => false,
                            'autoincrement' => false
                         ));

        // optional paste visibility
        $this->hasColumn('visibility', 'enum', 4,
                         array(
                            'values' => array(
                                'all', 'member', 'me'
                            )
                         ));

        // the paste itself
        $this->hasColumn('paste', 'string', null,
                         array(
                            'fixed' => false,
                            'primary' => false,
                            'notnull' => true,
                            'autoincrement' => false
                         ));
    }

    /**
     * Sets up model behaviour
     * 
     * @return void
     */
    public function setUp()
    {
        parent::setUp();
        
        $this->hasOne('Kaypaste_User',
                      array(
                          'local' => 'username',
                          'foreign' => 'username',
                          'onUpdate' => 'cascade',
                          'onDelete' => 'set null'
                      ));
        
        $this->actAs('Timestampable');
        $this->actAs('SoftDelete');
        $this->actAs('Searchable',
                     array(
                        'fields' => array('type',
                                          'description',
                                          'author',
                                          'paste')
                     ));
        /*
        $this->actAs('Sluggable', array('unique'    => true,
                                        'fields'    => array('description'),
                                        'canUpdate' => true));
        */
    }
}
